//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using System.Text;
using System.Collections;
using System.Collections.Specialized;

namespace CommunityServer.Controls
{
	public abstract class ContentScroller : Control
	{
		#region Properties

		public virtual int ScrollAnimationStepTimeout
		{
			get
			{
				object state = ViewState["ScrollAnimationStepTimeout"];
				if (state == null)
					return 49;
				else
					return (int) state;
			}
			set
			{
				ViewState["ScrollAnimationStepTimeout"] = value;
			}
		}

		public virtual double ScrollAnimationStepAcceleration
		{
			get
			{
				object state = ViewState["ScrollAnimationStepAcceleration"];
				if (state == null)
					return 0.8;
				else
					return (double) state;
			}
			set
			{
				ViewState["ScrollAnimationStepAcceleration"] = value;
			}
		}

		public virtual double ScrollAnimationInitialStepDistanceDivisor
		{
			get
			{
				object state = ViewState["ScrollAnimationInitialStepDistanceDivisor"];
				if (state == null)
					return 4;
				else
					return (double) state;
			}
			set
			{
				ViewState["ScrollAnimationInitialStepDistanceDivisor"] = value;
			}
		}

		public virtual string CssClass
		{
			get
			{
				string state = (string) ViewState["CssClass"];
				if (state == null)
					return String.Empty;
				else
					return state;
			}
			set
			{
				ViewState["CssClass"] = value;
			}
		}

		protected abstract string ContentTemplate {get;}

		protected abstract string[,] ScrollerData { get; }

		#endregion

		#region Event Handlers

		protected override void OnPreRender(EventArgs e)
		{
			if (!this.Page.IsClientScriptBlockRegistered(this.GetType().FullName))
				this.Page.RegisterClientScriptBlock(this.GetType().FullName, string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",this.ResolveUrl("~/utility/contentscroller.js")));

			StringBuilder script = new StringBuilder();
			script.Append("<script language=\"javascript\">var ");
			script.Append(this.ClientID);
			script.Append("=new Scroller('");
			script.Append(this.ClientID);
			script.Append("','");
			script.Append(this.ClientID);
			script.Append("','");
			script.Append(this.ContentTemplate.Replace("'", "\\'").Replace("</script>", "\\<\\/script\\>"));
			script.Append("',[");
			
			int i, j;
			for (i = 0; i <= this.ScrollerData.GetUpperBound(0); i++)
			{
				script.Append("[");

				for (j = 0; j <= this.ScrollerData.GetUpperBound(1); j++)
				{
					script.Append("'");
					script.Append(this.ScrollerData[i, j].Replace("'", "\\'").Replace("</script>", "\\<\\/script\\>"));
					script.Append("'");

					if (j < this.ScrollerData.GetUpperBound(1))
						script.Append(",");
				}

				script.Append("]");

				if (i < this.ScrollerData.GetUpperBound(0))
					script.Append(",");
			}

			script.Append("],");
			script.Append(this.ScrollAnimationStepTimeout.ToString());
			script.Append(",");
			script.Append(this.ScrollAnimationStepAcceleration.ToString());
			script.Append(",");
			script.Append(this.ScrollAnimationInitialStepDistanceDivisor.ToString());
			script.Append(");</script>");

			this.Page.RegisterStartupScript(this.ClientID, script.ToString());		

			base.OnPreRender(e);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			writer.Write("<div class=\"");
			writer.Write(this.CssClass);
			writer.Write("\"><div id=\"");
			writer.Write(this.ClientID);
			writer.Write("\" style=\"overflow: hidden; position: relative; width: 100%; height: 100%;\">&nbsp;</div></div>");
		}

		#endregion
	}
}
